package com.ask.controller.admin;

import com.ask.dto.DishDTO;
import com.ask.dto.DishQueryDTO;
import com.ask.entity.Dish;
import com.ask.repository.DishRepository;
import com.ask.result.PageResult;
import com.ask.result.Result;
import com.ask.service.DishService;
import com.ask.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    //@CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveDish(dishDTO);
        cleanCache("dishCache::" + dishDTO.getCategoryId());
        return Result.success();
    }

    /**
     * 根据id查询菜品及口味信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getByIdWithFlavor(@PathVariable Long id) {
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 菜品分页查询
     * @param dishQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishQueryDTO dishQueryDTO) {
        PageResult pageResult = dishService.pageQuery(dishQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> getByCategory(Long categoryId) {
        List<Dish> dishes = dishService.getByCategory(categoryId);
        return Result.success(dishes);
    }


    /**
     * 菜品起售、停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status, @RequestParam Long id){
        log.info("菜品起售停售，id, ");
        dishService.startOrStop(status, id);
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @CacheEvict(cacheNames = "dishCache", key = "#dishDTO.categoryId")
    public Result updateWithFlavor(@RequestBody DishDTO dishDTO) {
        dishService.update(dishDTO);
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(cacheNames = "dishCache", allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        dishService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @Autowired
    private DishRepository dishRepository;
    @GetMapping("/listByName")
    public Result getByName(String name) {
        List<Dish> dishes = dishRepository.findByName(name);
        return Result.success(dishes);
    }
}
